package com.mall4j.cloud.product.repository;

import com.mall4j.cloud.product.model.BrowseHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
@Repository
public interface BrowseHistoryRepository extends JpaRepository<BrowseHistory, Long> {
   List<BrowseHistory> findAllByUserIdOrderByBrowseTimeDesc(Long userId);
   @Modifying
   @Transactional
   @Query(value = "DELETE FROM browse_history WHERE (product_id, create_time) NOT IN (SELECT product_id,create_time FROM(SELECT product_id, MAX(create_time) AS create_time FROM browse_history GROUP BY product_id) AS newTable)",nativeQuery = true)
   void deleteHistory();
}
